define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'user/user/index?type='+Config.type,
                    add_url: 'user/user/add?type='+Config.type,
                    vip_url:"user/user/vip",
                    vip_try_url:"user/user/vip_try",
                    edit_url: 'user/user/edit',
                    del_url: 'user/user/del',
                    import_url: 'user/user/import',
                    multi_url: 'user/user/multi',
                    table: 'user',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'user.id',
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id'), sortable: true, operate: false},
                        {field: 'school.name', title: __('学校名称'), operate: false},
                        {field: 'grade.gradeName', title: __('年级'), operate: false},
                        {field: 'grade_class.name', title: __('班级'), operate: false},

                        {field: 'school_id',visible:false, title: __('学校'), formatter: Table.api.formatter.normal, searchList: Config.schoolConfig},
                        {field: 'grade_id',visible:false, title: __('年级'), formatter: Table.api.formatter.normal, searchList: Config.gradeConfig},
                        {field: 'class_id',visible:false, title: __('班级'), formatter: Table.api.formatter.normal, searchList: Config.classConfig},



                        {field: 'username', title: __('Username'), operate: false},
                        {field: 'nickname', title: __('Nickname'), operate:  'LIKE'},
                        // {field: 'email', title: __('Email'), operate: 'LIKE'},
                        {field: 'mobile', title: __('Mobile'), operate: 'LIKE'},
                        // {field: 'is_vip', title: __('是否会员'), formatter: Table.api.formatter.toggle, searchList: {1: __('是'), 0: __('否')}},
                        {field: 'vip_config.type_desc', title: __('会员类型'), formatter: Table.api.formatter.normal,operate: false},
                        {
                            field: 'buttons',
                            width: "120px",
                            title: __('会员'),
                            table: table,
                            events: Table.api.events.operate,
                            operate: false,
                            buttons: [
                                {
                                    name: 'detail',
                                    text: __('成为VIP'),
                                    title: __('成为VIP'),
                                    classname: 'btn btn-xs btn-primary btn-dialog',
                                    icon: 'fa fa-list',
                                    url: 'user/user/vip',
                                    callback: function (data) {
                                        Layer.alert("接收到回传数据：" + JSON.stringify(data), {title: "回传数据"});
                                    },
                                    visible: function (row) {
                                        //返回true时按钮显示,返回false隐藏
                                        return !row.is_vip;
                                    }
                                },
                                {
                                    name: 'detail',
                                    text: __('试用VIP'),
                                    title: __('试用VIP'),
                                    classname: 'btn btn-xs btn-primary btn-dialog',
                                    icon: 'fa fa-list',
                                    url: 'user/user/vip_try',
                                    callback: function (data) {
                                        Layer.alert("接收到回传数据：" + JSON.stringify(data), {title: "回传数据"});
                                    },
                                    visible: function (row) {
                                        //返回true时按钮显示,返回false隐藏
                                        return row.can_try;
                                    }
                                },
                                {
                                    name: 'ajax',
                                    text: __('取消VIP'),
                                    title: __('取消VIP'),
                                    classname: 'btn btn-xs btn-success btn-magic btn-ajax',
                                    icon: 'fa fa-magic',
                                    url: 'user/user/vip_cancel',
                                    confirm: '确定取消VIP？',
                                    success: function (data, ret) {
                                        Layer.alert(ret.msg );
                                        window.location.reload();
                                        //如果需要阻止成功提示，则必须使用return false;
                                        return true;
                                    },
                                    error: function (data, ret) {
                                        console.log(data, ret);
                                        Layer.alert(ret.msg);
                                        return false;
                                    },
                                    visible: function (row) {
                                        //返回true时按钮显示,返回false隐藏
                                        return row.is_vip;
                                    }
                                },{
                                    name: 'ajax',
                                    text: __('取消试用'),
                                    title: __('取消试用'),
                                    classname: 'btn btn-xs btn-success btn-magic btn-ajax',
                                    icon: 'fa fa-magic',
                                    url: 'user/user/vip_try_cancel',
                                    confirm: '确定取消VIP？',
                                    success: function (data, ret) {
                                        Layer.alert(ret.msg );
                                        window.location.reload();
                                        //如果需要阻止成功提示，则必须使用return false;
                                        return true;
                                    },
                                    error: function (data, ret) {
                                        console.log(data, ret);
                                        Layer.alert(ret.msg);
                                        return false;
                                    },
                                    visible: function (row) {
                                        //返回true时按钮显示,返回false隐藏
                                        return row.can_cancel_try;
                                    }
                                },
                            ],
                            formatter: Table.api.formatter.buttons
                        },
                        {field: 'vip_type',visible:false, title: __('会员类型'), formatter: Table.api.formatter.normal, searchList: Config.vipConfig},
                        {field: 'vip_end_time', title: __('会员到期时间'), formatter: Table.api.formatter.datetime, operate: false, addclass: 'datetimerange', sortable: true},
                        {field: 'avatar', title: __('Avatar'), events: Table.api.events.image, formatter: Table.api.formatter.image, operate: false},
                        {field: 'status', title: __('Status'), formatter: Table.api.formatter.status, searchList: {normal: __('Normal'), hidden: __('Hidden')},operate: false},
                        {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate},

                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
            $('.btn-vip').click(function () {
                var idArr =table.bootstrapTable('getSelections');
                var ids = '';
                if(idArr){
                    $.each(idArr,function(index,value){
                        ids = ids + value.id  + ',' ;
                    });
                }
                Fast.api.open('user/user/vip?ids='+ids, '会员', '');
            });
        },
        add: function () {

            $("#c-class_id").data("params", function (obj) {
                //obj为SelectPage对象
                return {custom:{grade_id: $("#c-grade_id").val()}};
            });

            // $("#c-class_id").data("params", function (obj) {
            //     return { custom: { grade_id: ['in',$("#c-grade_id").val() ]} };
            // });
            Controller.api.bindevent();
        },
        edit: function () {
            $("#c-class_id").data("params", function (obj) {
                //obj为SelectPage对象
                return {custom:{grade_id: $("#c-grade_id").val()}};
            });
            Controller.api.bindevent();
        },
        vip: function () {
            Controller.api.bindevent();
        },
        vip_try: function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});

function changeclass(){
    $("#c-class_id").val(0);
}